package com.samsung.spen.sshare.explorer.util;

import java.io.File;
import java.util.ArrayList;

import android.content.Context;
import android.net.Uri;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.samsung.spen.sshare.R;
import com.samsung.spen.sshare.explorer.viewholders.ViewHolder;

public class FileAdapter extends ArrayAdapter<FileExtend> implements Runnable {

	Context context;
	int viewResource;
	ArrayList<FileExtend> values;

	Handler mHandler; // for update UI

	Thread bindImage;
	ArrayList<ViewHolder> listHolder = new ArrayList<ViewHolder>();

	FileExtend fileExtendCurrent;

	public FileAdapter(Context context, Handler handler, int resource,
			ArrayList<FileExtend> values) {
		super(context, resource, values);

		this.context = context;
		this.viewResource = resource;
		this.values = values;
		this.mHandler = handler;

		bindImage = new Thread(this);
	}

	@Override
	public FileExtend getItem(int position) {
		return values.get(position);
	}

	@Override
	public int getPosition(FileExtend item) {

		for (int i = 0; i < values.size(); ++i) {

			if (values.get(i) == item) {
				return i;
			}
		}
		return -1;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;

		if (view == null) {

			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

			view = inflater.inflate(this.viewResource, parent, false);
			ViewHolder viewHolder = new ViewHolder();

			viewHolder.image = (ImageView) view.findViewById(R.id.image);
			viewHolder.title = (TextView) view.findViewById(R.id.title);
			viewHolder.desc = (TextView) view.findViewById(R.id.desc);
			viewHolder.checkbox = (CheckBox) view.findViewById(R.id.checkbox);
			viewHolder.imageView = (ImageView) view.findViewById(R.id.ivArrow);

			viewHolder.checkbox.setVisibility(View.INVISIBLE);
			viewHolder.imageView.setVisibility(View.INVISIBLE);

			view.setTag(viewHolder);
			listHolder.add(viewHolder);
		}

		final ViewHolder holder = (ViewHolder) view.getTag();

		final FileExtend data = values.get(position);
		fileExtendCurrent = data;
		holder.setData(data);

		// set title
		String title = data.getFile().getName();
		holder.title.setText(title);
		holder.title.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				data.flag_choosed = !data.flag_choosed;
				holder.checkbox.setChecked(data.flag_choosed);
			}
		});

		// set description

		holder.desc.setVisibility(View.VISIBLE);

		if (data.getFile().isDirectory()) {

			holder.checkbox.setVisibility(View.INVISIBLE);
			holder.file.flag_choosed = false;

			int folders = 0;
			int files = 0;

			try {

				for (File f : data.getFile().listFiles()) {

					if (f == null)
						continue;

					if (f.isDirectory())
						folders++;
					else
						files++;
				}

			} catch (NullPointerException e) {
				e.printStackTrace();
			}

			if (data.getFile().isHidden()) {

				holder.desc.setText("Hidden folder.");
				holder.image.setImageResource(R.drawable.exp_folder_hidden);
			} else {

				if (files > 0 || folders > 0) {

					holder.desc.setText(folders + " folders, " + files
							+ " files.");
					holder.image.setImageResource(R.drawable.exp_folder_empty);
				} else {

					holder.desc.setText("Empty folder");
					holder.image.setImageResource(R.drawable.exp_folder_empty);
				}
			}

			// if (files > 0 || folders > 0) {
			//
			// holder.desc.setText(folders + " folders, " + files + " files.");
			// holder.image.setImageResource(R.drawable.folders_blue);
			// } else {
			// holder.desc.setText("Empty");
			// holder.image.setImageResource(R.drawable.folder_hidden);
			// }

			holder.imageView.setVisibility(View.VISIBLE);
			// view.setBackgroundColor(Color.TRANSPARENT);

		} else {

			// checkbox for files
			holder.imageView.setVisibility(View.GONE);
			holder.checkbox.setVisibility(View.VISIBLE);
			holder.checkbox
					.setOnCheckedChangeListener(new OnCheckedChangeListener() {

						public void onCheckedChanged(CompoundButton checkBox,
								boolean isChecked) {

							if (isChecked) {
								data.flag_choosed = true;
							} else {
								data.flag_choosed = false;
							}

							SConfig.FLAG_SELECT_ALL = false;
						}
					});

			// set icon for files
			holder.image.setImageResource(R.drawable.exp_file_blank);
			holder.image.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {

					SConfig.FLAG_SELECT_ALL = false;
					data.flag_choosed = !data.flag_choosed;
					holder.checkbox.setChecked(data.flag_choosed);
				}
			});

			int index = title.lastIndexOf(".");
			String extStr = title.substring(index + 1);
			int idResource = FileUtil.getInstance().getReourceFile(extStr);
			holder.image.setImageResource(idResource);

			if (!bindImage.isAlive())
				bindImage.start();

			// long sizeFile = data.getFile().length();
			// long devide = 1;
			// String unit = "Kb";
			//
			// if (sizeFile > 1E11) {
			// devide = 1000000;
			// unit = "GB";
			//
			// } else if (sizeFile > 1E8) {
			//
			// devide = 1000;
			// unit = "MB";
			//
			// } else if (sizeFile > 1E5) {
			//
			// devide = 1000;
			// unit = "KB";
			// }
			//
			// holder.desc.setText(Integer.valueOf(String.valueOf(data.getFile()
			// .length() / devide))
			// + unit);

			holder.desc.setText(data.getSize());

			// view.setBackgroundColor(Color.YELLOW);
			holder.desc.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {

					SConfig.FLAG_SELECT_ALL = false;
					data.flag_choosed = !data.flag_choosed;
					holder.checkbox.setChecked(data.flag_choosed);
				}
			});

			// neu chon check tat ca cac checkbox thi set co nay la true, sau do
			// set
			// tng cai mot
			if (SConfig.FLAG_SELECT_ALL) {

				for (FileExtend item : values) {
					item.flag_choosed = true;
				}
			}

			holder.checkbox.setChecked(data.flag_choosed);

		}

		// neu chon check tat ca cac checkbox thi set co nay la true, sau do set
		// tng cai mot
		// if (SConfig.FLAG_SELECT_ALL) {
		//
		// for (FileExtend item : values) {
		// item.flag_choosed = true;
		// }
		// }
		//
		// holder.checkbox.setChecked(data.flag_choosed);

		return view;
	}

	public void setImage(final ViewHolder holder, final FileExtend file,
			final int idResource) {

		if (idResource == R.drawable.exp_file_image) {

			if (mHandler != null) {

				mHandler.post(new Runnable() {

					public void run() {
						try {
							holder.image.setImageURI(Uri.fromFile(file
									.getFile()));
						} catch (OutOfMemoryError e) {
							holder.image.setImageResource(idResource);
						}
					}
				});
			}
		}
	}

	public void run() {

		while (true) {

			for (ViewHolder holder : listHolder) {

				FileExtend file = holder.getFile();
				int index = file.getFile().getName().lastIndexOf(".");
				String extStr = file.getFile().getName().substring(index + 1);
				int idResource = FileUtil.getInstance().getReourceFile(extStr);
				setImage(holder, holder.getFile(), idResource);

				try {
					Thread.sleep(0);
				} catch (InterruptedException e) {
				}

			}

			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
			}

			try {

				Thread.sleep(1500);
			} catch (InterruptedException e) {
			}

		}
	}

	// class ViewHolder {
	//
	// ImageView image;
	// TextView title;
	// TextView desc;
	// CheckBox checkbox;
	// ImageView imageView;
	//
	// FileExtend file;
	//
	// public void setData(FileExtend file) {
	// this.file = file;
	// }
	//
	// public FileExtend getFile() {
	// return file;
	// }
	// }

	public String getDirParent() {

		if (fileExtendCurrent != null) {

			return fileExtendCurrent.getFile().getParent();
		}
		return null;
	}

}
